<?php
namespace TennisTournament\Controller;

use TennisTournament\Model\Match;
use TennisTournament\Model\StageOfTournamentEnum;
use TennisTournament\Model\WinnerEnum;
use TennisTournament\Service\MatchService;
use TennisTournament\Service\PlayerService;
use Zend\Json\Json;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController {

    /** @var MatchService */
    protected $matchService;

    /** @var PlayerService */
    protected $playerService;

    const FORWARD_INDEX = 'tennis-tournament/index/index';

    /**
     * @param MatchService $matchService
     * @param PlayerService $playerService
     */
    public function __construct(MatchService $matchService, PlayerService $playerService) {
        $this->matchService = $matchService;
        $this->playerService = $playerService;
    }

    public function indexAction() {
        $today = new \DateTime();
        $yesterday = (new \DateTime())->sub(new \DateInterval('P1D'));
        $matches = array();
        $output = array();

        $matchesToday = $this->matchService->getAllMatchesByDate($today);
        for ($i = 0; $i < count($matchesToday); $i++) {
            $matchesToday[$i] = $matchesToday[$i]->toArray();
        }
        $matches['matchesToday'] = $matchesToday;

        $matchesYesterday = $this->matchService->getAllMatchesByDate($yesterday);
        for ($i = 0; $i < count($matchesYesterday); $i++) {
            $matchesYesterday[$i] = $matchesYesterday[$i]->toArray();
        }
        $matches['matchesYesterday'] = $matchesYesterday;

        $output['matches'] = $matches;
        $winners = null;
        if ($this->matchService->isPlayedAllMatchesInStageOfTournament(StageOfTournamentEnum::FINAL_STAGE)) {
            $winners = array();
            /** @var $final Match */
            $final = $this->matchService->fetchAllWithStageOfTournament(StageOfTournamentEnum::FINAL_STAGE)[0];
            if ($final->getWinner() == WinnerEnum::HOST){
                $winners['first'] = $final->getPlayerHost()->toArray();
                $winners['second'] = $final->getPlayerGuest()->toArray();
            } else {
                $winners['first'] = $final->getPlayerGuest()->toArray();
                $winners['second'] = $final->getPlayerHost()->toArray();
            }
            /** @var $thirdPlace Match */
            $thirdPlace = $this->matchService->fetchAllWithStageOfTournament(StageOfTournamentEnum::THIRD_PLACE)[0];
            if ($thirdPlace->getWinner() == WinnerEnum::HOST) {
                $winners['third'] = $thirdPlace->getPlayerHost()->toArray();
            } else {
                $winners['third'] = $thirdPlace->getPlayerGuest()->toArray();
            }

            $winners['bestScorer'] = $this->playerService->getBestScorer()->toArray();
        }
        $output['winners'] = $winners;

        $viewModel = new ViewModel();
        $viewModel->setTemplate(self::FORWARD_INDEX);

        return $viewModel->setVariables(array("index" => Json::encode($output)));
    }

}
